import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/pages/Home.vue'
import About from '@/pages/About.vue'
import Login from '@/pages/Login.vue'
import Layout from '@/pages/Layout.vue'
import Screen from '@/pages/Screen.vue'

import nprogress from 'nprogress'
import 'nprogress/nprogress.css';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: Home
        },
        {
          path: '/about',
          component: About
        }
      ]
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/screen',
      component: Screen
    }
  ],
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    }
  }
})

router.beforeEach((to, from, next) => {
  nprogress.start();
  // document.title = to.meta.title ? to.meta.title:'test'
  let getToken = localStorage.getItem("TOKEN")
  if (to.path == "/login") return next()
  if (getToken) {
    next()
  } else {
    next("/login")
  }
})

router.afterEach((to, from) => {
  nprogress.done();
})
export default router